﻿using WpfMvvmTemplatePrismHC.Common;
using WpfMvvmTemplatePrismHC.Models.BindingModels;
using WpfMvvmTemplatePrismHC.Models.ProgramModel;
using WpfMvvmTemplatePrismHC.Tools.Events;

namespace WpfMvvmTemplatePrismHC.ViewModels.UserControls
{
    public class ViewCViewModel : BindableBase
    {
        /// <summary>
        /// ViewCModel模型
        /// </summary>
        public ViewCModel? ViewCModels { get; set; }

        private readonly IEventAggregator? eventAggregator;
        public IContainerProvider? containerProvider { get; }
        public DelegateCommand? PublishCommand { get; set; }

        public ViewCViewModel()
        {
            ViewCModels = GlobalStaticData.IsDesignTime ? new ViewCModel() : containerProvider.Resolve<ProgramModel>().ViewCModels;
        }

        public ViewCViewModel(IContainerProvider containerProvider, IEventAggregator eventAggregator)
        {
            ViewCModels = GlobalStaticData.IsDesignTime ? new ViewCModel() : containerProvider.Resolve<ProgramModel>().ViewCModels;
            this.containerProvider = containerProvider;
            this.eventAggregator = eventAggregator;
            PublishCommand = new DelegateCommand(PublishEvent);
			
        }

        private void PublishEvent()
        {
            //发布消息
            if (int.TryParse(ViewCModels?.Message, out int res))
            {
                eventAggregator?.GetEvent<ValueChangedEvent<int>>().Publish(new ValueChangedEventArgs<int> { NewValue = res });
            }
        }
    }
}